{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.24677549  0.92405374  0.24979447  0.13810811  0.29681337  0.66402811\n",
      "  0.76071998  0.14816409  0.14230114  0.72744816  0.73856635  0.92889219\n",
      "  0.45183418  0.17475408  0.71591186  0.67515803  0.493655    0.47804297\n",
      "  0.78537153  0.865365    0.48333726  0.11769709  0.61944043  0.64947429\n",
      "  0.25217882  0.03975352  0.69443903  0.96608953  0.79202691  0.83284096\n",
      "  0.68462775  0.55129531  0.38569836  0.47272957  0.15646664  0.45156487\n",
      "  0.20258141  0.16575228  0.52476107  0.57982011  0.94338504  0.34419686\n",
      "  0.32298604  0.6428689   0.80794093  0.16607068  0.32735392  0.14916714\n",
      "  0.99944881  0.61563756  0.29467264  0.06290676  0.54856672  0.14988116\n",
      "  0.50609289  0.34658113  0.30249508  0.18155225  0.24046884  0.16321492\n",
      "  0.79349597  0.87081089  0.79272167  0.22654705  0.38923366  0.7741124\n",
      "  0.21557469  0.09041074  0.24625419  0.70166794  0.80283565  0.73813726\n",
      "  0.13363019  0.13856399  0.40649858  0.02076579  0.89907183  0.67392912\n",
      "  0.61014719  0.65481784  0.90478184  0.78948937  0.26635238  0.47260414\n",
      "  0.62468809  0.01868854  0.54823415  0.31121734  0.17715625  0.28283398\n",
      "  0.73358784  0.3278326   0.62853698  0.8152582   0.33690918  0.82175916\n",
      "  0.1547397   0.62668824  0.1349279   0.02953855  0.62764004  0.2142891\n",
      "  0.86744031  0.10326298  0.3578529   0.3056195   0.50984845  0.47838632\n",
      "  0.3521898   0.57891229  0.45978155  0.3541714   0.70309612  0.65534563\n",
      "  0.5637022   0.31434735  0.04343127  0.70625432  0.90912951  0.58874363\n",
      "  0.54978335  0.66687932  0.88291594  0.32627793  0.08780816  0.55819189\n",
      "  0.39497512  0.47818115  0.65846136  0.91050164  0.31384655  0.793125\n",
      "  0.73015303  0.15732991  0.46996432  0.71263565  0.90580429  0.98880671\n",
      "  0.22949558  0.98390058  0.19275425  0.05221787  0.66930987  0.02837141\n",
      "  0.58548345  0.20868075  0.34541248  0.6491709   0.42785786  0.75818783\n",
      "  0.87756747  0.00493606  0.51160478  0.37714171  0.341784    0.54733348\n",
      "  0.32083522  0.29846449  0.62337769  0.32073091  0.75748298  0.74817468\n",
      "  0.08074967  0.81106714  0.33932364  0.45475766  0.1222747   0.79498144\n",
      "  0.0466393   0.71719231  0.07523068  0.10374977  0.50083196  0.64185793\n",
      "  0.05127226  0.98309108  0.79051837  0.239635    0.85748956  0.38011724\n",
      "  0.69697947  0.15368236  0.86598016  0.46753208  0.85455084  0.23538378\n",
      "  0.63913148  0.92190399  0.03778038  0.06241063  0.60575462  0.52175306\n",
      "  0.03520609  0.82636492  0.82616208  0.90222073  0.91633446  0.47816589\n",
      "  0.28073368  0.16790273] [ 0.52209794  1.13164837  0.52481502  0.4242973   0.56713203  0.8976253\n",
      "  0.98464799  0.43334768  0.42807103  0.95470334  0.96470972  1.13600297\n",
      "  0.70665076  0.45727867  0.94432067  0.90764222  0.7442895   0.73023867\n",
      "  1.00683438  1.0788285   0.73500353  0.40592738  0.85749638  0.88452686\n",
      "  0.52696094  0.33577816  0.92499512  1.16948057  1.01282422  1.04955686\n",
      "  0.91616498  0.79616577  0.64712852  0.72545662  0.44081998  0.70640838\n",
      "  0.48232327  0.44917705  0.77228496  0.8218381   1.14904654  0.60977718\n",
      "  0.59068744  0.87858201  1.02714684  0.44946361  0.59461853  0.43425043\n",
      "  1.19950393  0.85407381  0.56520538  0.35661608  0.79371005  0.43489305\n",
      "  0.7554836   0.61192302  0.57224557  0.46339702  0.51642195  0.44689343\n",
      "  1.01414637  1.0837298   1.01344951  0.50389234  0.65031029  0.99670116\n",
      "  0.49401722  0.38136967  0.52162877  0.93150114  1.02255209  0.96432353\n",
      "  0.42026717  0.42470759  0.66584872  0.31868921  1.10916465  0.90653621\n",
      "  0.84913247  0.88933606  1.11430365  1.01054044  0.53971715  0.72534373\n",
      "  0.86221928  0.31681969  0.79341074  0.58009561  0.45944062  0.55455058\n",
      "  0.96022906  0.59504934  0.86568329  1.03373238  0.60321826  1.03958324\n",
      "  0.43926573  0.86401942  0.42143511  0.32658469  0.86487603  0.49286019\n",
      "  1.08069628  0.39293668  0.62206761  0.57505755  0.75886361  0.73054768\n",
      "  0.61697082  0.82102106  0.7138034   0.61875426  0.93278651  0.88981107\n",
      "  0.80733198  0.58291261  0.33908814  0.93562889  1.11821656  0.82986926\n",
      "  0.79480502  0.90019139  1.09462434  0.59365014  0.37902734  0.8023727\n",
      "  0.65547761  0.73036303  0.89261522  1.11945148  0.58246189  1.0138125\n",
      "  0.95713773  0.44159692  0.72296789  0.94137208  1.11522386  1.18992604\n",
      "  0.50654602  1.18551052  0.47347882  0.34699608  0.90237888  0.32553427\n",
      "  0.82693511  0.48781267  0.61087123  0.88425381  0.68507207  0.98236905\n",
      "  1.08981073  0.30444245  0.7604443   0.63942754  0.6076056   0.79260013\n",
      "  0.5887517   0.56861804  0.86103992  0.58865781  0.98173468  0.97335721\n",
      "  0.3726747   1.02996043  0.60539128  0.70928189  0.41004723  1.0154833\n",
      "  0.34197537  0.94547308  0.36770761  0.39337479  0.75074877  0.87767213\n",
      "  0.34614503  1.18478197  1.01146653  0.5156715   1.0717406   0.64210552\n",
      "  0.92728153  0.43831413  1.07938214  0.72077887  1.06909576  0.5118454\n",
      "  0.87521834  1.12971359  0.33400234  0.35616956  0.84517916  0.76957776\n",
      "  0.33168548  1.04372843  1.04354587  1.11199865  1.12470102  0.7303493\n",
      "  0.55266032  0.45111246]\n",
      "0 [0.29487893, 0.17135613]\n",
      "1 [0.43851006, 0.26388764]\n",
      "2 [0.50670898, 0.31688586]\n",
      "3 [0.53733021, 0.34997442]\n",
      "4 [0.54927355, 0.37295619]\n",
      "5 [0.55197394, 0.39073014]\n",
      "6 [0.55014056, 0.40574652]\n",
      "7 [0.54612267, 0.4192327]\n",
      "8 [0.54109144, 0.43180537]\n",
      "9 [0.53562969, 0.44377622]\n",
      "10 [0.53002661, 0.4553045]\n",
      "11 [0.52442473, 0.46647328]\n",
      "12 [0.51889336, 0.47732747]\n",
      "13 [0.51346546, 0.48789287]\n",
      "14 [0.50815576, 0.49818569]\n",
      "15 [0.50296998, 0.50821722]\n",
      "16 [0.49790925, 0.51799613]\n",
      "17 [0.49297267, 0.52752995]\n",
      "18 [0.48815823, 0.5368253]\n",
      "19 [0.48346338, 0.54588836]\n",
      "20 [0.47888541, 0.55472517]\n",
      "21 [0.47442156, 0.56334138]\n",
      "22 [0.47006905, 0.57174253]\n",
      "23 [0.46582511, 0.579934]\n",
      "24 [0.46168706, 0.58792108]\n",
      "25 [0.45765227, 0.59570885]\n",
      "26 [0.45371816, 0.60330224]\n",
      "27 [0.44988221, 0.61070615]\n",
      "28 [0.44614199, 0.61792529]\n",
      "29 [0.44249511, 0.6249643]\n",
      "30 [0.43893921, 0.63182765]\n",
      "31 [0.43547207, 0.63851976]\n",
      "32 [0.43209144, 0.64504486]\n",
      "33 [0.42879516, 0.65140712]\n",
      "34 [0.42558116, 0.6576106]\n",
      "35 [0.42244735, 0.66365927]\n",
      "36 [0.41939175, 0.66955703]\n",
      "37 [0.41641241, 0.67530763]\n",
      "38 [0.4135074, 0.6809147]\n",
      "39 [0.41067487, 0.68638188]\n",
      "40 [0.40791303, 0.69171262]\n",
      "41 [0.40522012, 0.69691032]\n",
      "42 [0.40259442, 0.70197833]\n",
      "43 [0.40003422, 0.70691985]\n",
      "44 [0.39753792, 0.71173805]\n",
      "45 [0.3951039, 0.71643603]\n",
      "46 [0.39273062, 0.72101676]\n",
      "47 [0.39041659, 0.72548318]\n",
      "48 [0.38816029, 0.72983813]\n",
      "49 [0.38596031, 0.73408443]\n",
      "50 [0.38381523, 0.73822474]\n",
      "51 [0.38172367, 0.74226177]\n",
      "52 [0.3796843, 0.74619806]\n",
      "53 [0.37769583, 0.75003612]\n",
      "54 [0.37575698, 0.7537784]\n",
      "55 [0.3738665, 0.75742728]\n",
      "56 [0.3720232, 0.76098508]\n",
      "57 [0.37022591, 0.76445413]\n",
      "58 [0.36847344, 0.76783657]\n",
      "59 [0.36676472, 0.77113461]\n",
      "60 [0.36509866, 0.7743504]\n",
      "61 [0.36347416, 0.77748591]\n",
      "62 [0.3618902, 0.78054315]\n",
      "63 [0.36034578, 0.78352416]\n",
      "64 [0.35883987, 0.78643072]\n",
      "65 [0.35737157, 0.7892648]\n",
      "66 [0.35593989, 0.79202813]\n",
      "67 [0.35454395, 0.7947225]\n",
      "68 [0.35318282, 0.79734963]\n",
      "69 [0.35185567, 0.7999112]\n",
      "70 [0.35056165, 0.80240887]\n",
      "71 [0.34929991, 0.8048442]\n",
      "72 [0.34806967, 0.80721879]\n",
      "73 [0.34687009, 0.80953407]\n",
      "74 [0.34570047, 0.8117916]\n",
      "75 [0.34456006, 0.8139928]\n",
      "76 [0.34344807, 0.81613904]\n",
      "77 [0.34236386, 0.81823176]\n",
      "78 [0.34130669, 0.82027227]\n",
      "79 [0.34027591, 0.82226181]\n",
      "80 [0.33927086, 0.8242017]\n",
      "81 [0.33829087, 0.8260932]\n",
      "82 [0.33733535, 0.82793748]\n",
      "83 [0.33640367, 0.82973576]\n",
      "84 [0.33549523, 0.83148915]\n",
      "85 [0.33460948, 0.83319879]\n",
      "86 [0.33374584, 0.83486575]\n",
      "87 [0.33290374, 0.83649117]\n",
      "88 [0.33208263, 0.838076]\n",
      "89 [0.33128202, 0.83962125]\n",
      "90 [0.33050141, 0.84112799]\n",
      "91 [0.32974026, 0.84259713]\n",
      "92 [0.32899809, 0.84402961]\n",
      "93 [0.32827446, 0.84542632]\n",
      "94 [0.32756889, 0.84678817]\n",
      "95 [0.32688093, 0.84811604]\n",
      "96 [0.32621014, 0.84941077]\n",
      "97 [0.32555607, 0.8506732]\n",
      "98 [0.32491833, 0.85190409]\n",
      "99 [0.3242965, 0.85310429]\n",
      "100 [0.32369021, 0.85427457]\n",
      "101 [0.32309902, 0.85541564]\n",
      "102 [0.32252258, 0.85652822]\n",
      "103 [0.32196054, 0.85761303]\n",
      "104 [0.32141253, 0.85867077]\n",
      "105 [0.32087821, 0.85970211]\n",
      "106 [0.3203572, 0.8607077]\n",
      "107 [0.31984919, 0.8616882]\n",
      "108 [0.31935388, 0.86264426]\n",
      "109 [0.31887093, 0.86357647]\n",
      "110 [0.31840003, 0.86448538]\n",
      "111 [0.31794086, 0.86537164]\n",
      "112 [0.31749314, 0.86623579]\n",
      "113 [0.3170566, 0.86707836]\n",
      "114 [0.31663096, 0.86789989]\n",
      "115 [0.31621593, 0.86870092]\n",
      "116 [0.31581128, 0.86948198]\n",
      "117 [0.31541672, 0.87024355]\n",
      "118 [0.31503201, 0.8709861]\n",
      "119 [0.31465688, 0.87171012]\n",
      "120 [0.31429112, 0.87241608]\n",
      "121 [0.3139345, 0.87310445]\n",
      "122 [0.31358677, 0.8737756]\n",
      "123 [0.31324771, 0.87443]\n",
      "124 [0.31291714, 0.87506807]\n",
      "125 [0.3125948, 0.87569022]\n",
      "126 [0.31228051, 0.87629688]\n",
      "127 [0.31197405, 0.87688839]\n",
      "128 [0.31167525, 0.87746513]\n",
      "129 [0.3113839, 0.8780275]\n",
      "130 [0.31109983, 0.8785758]\n",
      "131 [0.31082284, 0.87911046]\n",
      "132 [0.31055275, 0.87963176]\n",
      "133 [0.31028941, 0.88014001]\n",
      "134 [0.31003264, 0.88063562]\n",
      "135 [0.30978227, 0.88111883]\n",
      "136 [0.30953816, 0.88159001]\n",
      "137 [0.30930012, 0.88204944]\n",
      "138 [0.30906805, 0.88249737]\n",
      "139 [0.30884176, 0.88293415]\n",
      "140 [0.30862111, 0.88336003]\n",
      "141 [0.30840597, 0.88377529]\n",
      "142 [0.30819619, 0.88418019]\n",
      "143 [0.30799165, 0.88457495]\n",
      "144 [0.30779225, 0.88495988]\n",
      "145 [0.30759779, 0.88533521]\n",
      "146 [0.30740818, 0.88570118]\n",
      "147 [0.30722332, 0.88605797]\n",
      "148 [0.30704308, 0.88640589]\n",
      "149 [0.30686733, 0.8867451]\n",
      "150 [0.30669597, 0.88707584]\n",
      "151 [0.30652887, 0.88739836]\n",
      "152 [0.30636594, 0.88771284]\n",
      "153 [0.30620709, 0.88801944]\n",
      "154 [0.30605221, 0.88831842]\n",
      "155 [0.30590117, 0.88860995]\n",
      "156 [0.30575392, 0.8888942]\n",
      "157 [0.30561033, 0.88917136]\n",
      "158 [0.30547032, 0.88944155]\n",
      "159 [0.30533379, 0.889705]\n",
      "160 [0.3052007, 0.8899619]\n",
      "161 [0.30507091, 0.89021242]\n",
      "162 [0.30494437, 0.89045668]\n",
      "163 [0.30482098, 0.8906948]\n",
      "164 [0.30470067, 0.89092702]\n",
      "165 [0.30458337, 0.89115345]\n",
      "166 [0.30446899, 0.89137423]\n",
      "167 [0.30435747, 0.89158946]\n",
      "168 [0.30424872, 0.89179933]\n",
      "169 [0.30414271, 0.89200395]\n",
      "170 [0.30403933, 0.89220351]\n",
      "171 [0.30393854, 0.89239806]\n",
      "172 [0.30384025, 0.89258778]\n",
      "173 [0.30374441, 0.89277273]\n",
      "174 [0.30365098, 0.8929531]\n",
      "175 [0.30355987, 0.89312893]\n",
      "176 [0.30347103, 0.89330041]\n",
      "177 [0.30338442, 0.89346761]\n",
      "178 [0.30329996, 0.89363062]\n",
      "179 [0.30321762, 0.89378959]\n",
      "180 [0.3031373, 0.89394456]\n",
      "181 [0.30305901, 0.89409566]\n",
      "182 [0.30298269, 0.894243]\n",
      "183 [0.30290824, 0.89438665]\n",
      "184 [0.30283567, 0.89452672]\n",
      "185 [0.30276492, 0.89466333]\n",
      "186 [0.30269593, 0.89479649]\n",
      "187 [0.30262867, 0.89492637]\n",
      "188 [0.30256304, 0.89505297]\n",
      "189 [0.30249909, 0.89517641]\n",
      "190 [0.30243671, 0.89529675]\n",
      "191 [0.30237591, 0.89541411]\n",
      "192 [0.30231664, 0.89552855]\n",
      "193 [0.30225882, 0.89564013]\n",
      "194 [0.30220246, 0.89574891]\n",
      "195 [0.30214751, 0.89585501]\n",
      "196 [0.30209392, 0.89595842]\n",
      "197 [0.30204168, 0.89605927]\n",
      "198 [0.30199072, 0.89615762]\n",
      "199 [0.30194104, 0.89625353]\n"
     ]
    }
   ],
   "source": [
    "#原始数据集\n",
    "x_data = np.random.rand(200)\n",
    "y_data = x_data*0.9+0.3\n",
    "print(x_data)\n",
    "#线性模型\n",
    "b = tf.Variable(0.)\n",
    "k = tf.Variable(0.)\n",
    "y = k*x_data+b\n",
    "\n",
    "#代价函数\n",
    "loss = tf.reduce_mean(tf.square(y-y_data))\n",
    "#优化器\n",
    "optimizer = tf.train.GradientDescentOptimizer(0.2)\n",
    "#最小化代价函数\n",
    "train = optimizer.minimize(loss)\n",
    "\n",
    "init = tf.global_variables_initializer()\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for step in range(200):\n",
    "        sess.run(train)\n",
    "        print(step,sess.run([b,k]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
